<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-home"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>类别列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-button @click="loadCategoryList" style="float: right">返回一级父级</el-button>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
<!--   id, name, parent_id, depth, keywords, sort, icon, enable, is_parent, is_display   -->
      <el-table-column prop="id" label="ID" align="center" width="50"></el-table-column>
      <el-table-column prop="name" label="类别名称" align="center" width="60"></el-table-column>
      <el-table-column prop="parentId" label="父级ID" align="center" width="180"></el-table-column>
      <el-table-column prop="depth" label="深度" align="center" width="100"></el-table-column>
      <el-table-column prop="keywords" label="关键词" align="center" width="180"></el-table-column>
      <el-table-column prop="sort" label="排序序号" align="center" width="60"></el-table-column>
      <el-table-column prop="icon" label="图标" align="center" width="60"></el-table-column>
      <el-table-column prop="isParent" label="是否父级" align="center" width="60"></el-table-column>
      <el-table-column prop="isDisplay" label="是否显示" align="center" width="60">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.isDisplay"
              @change="changeIsDisplay(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="grey">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="enable" label="是否启用" width="60">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="changeEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="grey">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="isParent" label="查看子级类别" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="viewChildLevel(scope.row.id)" :disabled="scope.row.isParent == 0">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
            @click="handleEdit(scope.row)" ></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
            @click="openHandleDelete(scope.row)" :disabled="scope.row.isParent == 1"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "CategoryListView",
  data() {
    return {
      tableData: [],
      childId: ''
    }
  },
  created(){
    console.log("created...")
  },
  mounted() {
    console.log("mounted...")
    this.loadCategoryList()
  },
  methods: {
    viewChildLevel(id){
      this.childId = id
      this.loadCategoryChildList(id)
    },
    changeIsDisplay(){
      this.$message("该功能正在研发中")
    },
    changeEnable(category) {
      let url = "http://localhost:9080/categories/" + category.id
      let tips = ["禁用","启用"]
      if(category.enable == 1){
        url += "/enable"
      }else {
        url += "/disable"
      }
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        console.log("responseBody=")
        console.log(responseBody)
        if(responseBody.state != 20000){

          let title = "操作失败"
          this.$alert(responseBody.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList()
            }
          })
        }else {
          this.$message.success(tips[category.enable] + "类别成功")
        }
      })

    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    //仅刷新一级父类的列表
    loadCategoryList(){
      console.log("loadCategoryList...")
      let url = "http://localhost:9080/categories"
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("response参数：",response)
        let responseBody = response.data;
        console.log("response的data参数：",responseBody)
        this.tableData = responseBody.data
      })
    },
    //刷新子类的列表
    loadCategoryChildList(id){
      console.log("loadCategoryList...")
      let url = "http://localhost:9080/categories/child/" +id
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then(response =>{
        console.log("response参数：",response)
        let responseBody = response.data;
        console.log("response的data参数：",responseBody)
        this.tableData = responseBody.data
      })
    },
    openHandleDelete(category) {
      this.$confirm('是否删除【'+category.name+'(id='+category.id+')】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.handleDelete(category)
      }).catch(() => {
      });
    },
    handleDelete(category){
      // alert("你点击了【"+name+"(id="+id+")】的删除按钮")
      let url = 'http://localhost:9080/categories/'+category.id+'/delete'
      this.axios
          .create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then(response=>{
        let responseBody = response.data
        if(responseBody.state != 20000){
          this.$message.error("删除失败")
        }
        this.loadCategoryChildList(this.childId)
      })
    },
    handleEdit(category){
      alert("你点击了【"+category.name+"(id="+category.id+")】的编辑按钮")
    }
  }
}
</script>

<style scoped>

</style>